package com.metaera.app.ui.popup;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.lxj.xpopup.core.BottomPopupView;
import com.metaera.app.R;
import com.metaera.app.data.network.ApiParam;
import com.metaera.app.ui.activities.SearchActivity;
import com.metaera.app.ui.fragments.SearchResultListFragment;
import com.metaera.app.ui.fragments.SynthesisSearchResultFragment;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

public class SearchFilterPopup extends BottomPopupView {

    public int orderType = ApiParam.SEARCH_ORDER_TYPE_NEW,
            timeType     = ApiParam.SEARCH_TIME_THREE_MONTHS;

    private RadioGroup groupOrder;
    private RadioGroup groupTime1;
    private RadioGroup groupTime2;

    private SearchActivity activity;

    boolean isOperate = false;


    public SearchFilterPopup(@NonNull Context context) {
        super(context);
    }

    public SearchFilterPopup(@NonNull SearchActivity activity, FragmentManager fm) {
        super(activity);
        this.activity = activity;
    }

    @Override
    protected int getImplLayoutId() {

        return R.layout.popup_search_filter;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
//        RadioGroup groupRule = findViewById(R.id.group_rule);
        groupOrder = findViewById(R.id.group_order);
        groupTime1 = findViewById(R.id.group_time_1);
        groupTime2 = findViewById(R.id.group_time_2);

        // 默认选择
        groupTime1.clearCheck();
        groupOrder.check(R.id.radio_latest_release);
        groupTime2.check(R.id.radio_three_months);

        // 重置
        findViewById(R.id.button_reset).setOnClickListener(v -> {
            orderType = ApiParam.SEARCH_ORDER_TYPE_NEW;
            timeType = ApiParam.SEARCH_TIME_THREE_MONTHS;
            groupTime1.clearCheck();
            groupOrder.check(R.id.radio_latest_release);
            groupTime2.check(R.id.radio_three_months);
        });

        // 关闭
        findViewById(R.id.button_close).setOnClickListener(v -> dismiss());

        // 确定
        findViewById(R.id.button_confirm).setOnClickListener(v -> {
            RadioButton orderButton = groupOrder.findViewById(groupOrder.getCheckedRadioButtonId());
            orderType = groupOrder.indexOfChild(orderButton) + 1;

            if (groupTime1.getCheckedRadioButtonId() != -1) {
                RadioButton time1 = groupTime1.findViewById(groupTime1.getCheckedRadioButtonId());
                timeType = groupTime1.indexOfChild(time1) + 1;
            }

            if (groupTime2.getCheckedRadioButtonId() != -1) {
                RadioButton time2 = groupTime2.findViewById(groupTime2.getCheckedRadioButtonId());
                timeType = groupTime2.indexOfChild(time2) + 4;
            }

            EditText etSearch = activity.findViewById(R.id.et_search);
            activity.renderSearchList(etSearch.getText().toString());

            dismiss();
        });

        RadioGroup.OnCheckedChangeListener onCheckedChangeListener = (group, checkedId) -> {
            if (isOperate) return;
            if (group == groupTime1) {
                isOperate = true;
                groupTime2.clearCheck();
                isOperate = false;
            } else {
                isOperate = true;
                groupTime1.clearCheck();
                isOperate = false;
            }
        };

        groupTime1.setOnCheckedChangeListener(onCheckedChangeListener);
        groupTime2.setOnCheckedChangeListener(onCheckedChangeListener);
    }

    @Override
    public void dismiss() {
        super.dismiss();

        groupOrder.postDelayed(() -> {

            if (orderType == 1) {
                groupOrder.check(R.id.radio_interactive);
            }

            if (orderType == 2) {
                groupOrder.check(R.id.radio_most_read);
            }

            if (orderType == 3) {
                groupOrder.check(R.id.radio_latest_release);
            }

            if (timeType == 1) {
                groupTime1.check(R.id.radio_day);
            }

            if (timeType == 2) {
                groupTime1.check(R.id.radio_week);
            }

            if (timeType == 3) {
                groupTime1.check(R.id.radio_month);
            }

            if (timeType == 4) {
                groupTime2.check(R.id.radio_three_months);
            }

            if (timeType == 5) {
                groupTime2.check(R.id.radio_year);
            }
        }, 500);

    }

}
